package com.bluemsun.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @author Steve Paul
 */
public class Login extends HttpServlet
{
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter pw = resp.getWriter();
        //获取输入进来的用户名和密码
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //读取文件中的用户名和密码,然后一一对应查找
        File users = new File(getServletContext().getRealPath("/") + "WEB-INF/classes/users");
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(users)));
        String input;
        String[] splitInput;
        boolean isOk = false;
        while ((input = br.readLine()) != null) {
            splitInput = input.split(" ");
            if (username.equals(splitInput[0]) && password.equals(splitInput[1])) {
                isOk = true;
                break;
            }
        }
        br.close();
        if(isOk) {
            pw.append("恭喜!您已登陆成功<br>");
        } else {
            pw.append("您的账号或密码有误<br>");
        }
        pw.close();
    }
}